// pages/xctask/detail.js

const util = require('../../utils/util.js');
const apiUrl = require('../../utils/apiUrl.js');
var http = require('../../utils/httpHelper.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    id: 0,
    detail: {},
    detail_content: {},
    detail_feedback: {},
    loaddinghidden: true,
    alertViewShow:false,
    feedback:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var id = options.id;
    if (!id) {
      wx.showModal({
        title: '加载失败！',
        success: function (res) {
          if (res.confirm) {
            wx.navigateBack();
          }
        }
      })
      return;
    }
    /**
     * 获取系统信息
     */
    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });

    // 显示 loading
    that.setData({
      id: id,
      loaddinghidden: false
    });

    that.loadData();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },
  loadData: function () {
    var that = this;
    var parameter = {};
    parameter['id'] = that.data.id;
    http.ApiGet(apiUrl.UrlXcTaskDetail, parameter, function (res) {
      wx.stopPullDownRefresh() //停止下拉刷新
      if (res.code == 1) {
        var detail = res.data;
        var detail_content = detail.content.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block;padding-top:2px;" ')
          .replace(/<section/g, '<div')
          .replace(/\/section>/g, '\div>');

        var detail_feedback = "";
        if (detail.feedback) {
          detail_feedback = detail.feedback.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block;padding-top:2px;" ')
            .replace(/<section/g, '<div')
            .replace(/\/section>/g, '\div>');
        }

        detail.feedback_list = detail.feedback_list.map(function (item) {
          item.feedback = item.feedback.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block;padding-top:2px;" ')
            .replace(/<section/g, '<div')
            .replace(/\/section>/g, '\div>');
          return item;
        })

        that.setData({
          loaddinghidden: true,
          detail: detail,
          detail_content: detail_content,
          detail_feedback: detail_feedback
        });
      } else {
        wx.showModal({
          title: res.msg
        });
      }
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.loadData();
  },
  copyTap: function (e) {
    var val = e.currentTarget.dataset.val;
    if (val.length > 0) {
      wx.setClipboardData({
        data: val,
        success: function (res) {
          console.log(res);
        }
      });
    }
  },
  contentTap: function () {
    var content = this.data.detail.content;
    content = content.replace(/<.*?>/g, "");
    wx.setClipboardData({
      data: content,
      success: function (res) {
        console.log(res);
      }
    });
  },
  feedbackTap: function () {
    var feedback = this.data.detail.feedback;
    feedback = feedback.replace(/<.*?>/g, "");
    wx.setClipboardData({
      data: feedback,
      success: function (res) {
        console.log(res);
      }
    });
  },
  doSubmit: function () {

    let that = this;

    wx.navigateTo({
      url: '../xctask/submit?task_id=' + that.data.id + '&task_title=' + that.data.detail.name
    })

    // that.setData({
    //   alertViewShow: true
    // })
  },
  feedbackContentInput: function (e) {
    this.setData({
      feedback: e.detail.value
    })
  },
  okBtnTap: function () {
    let that = this;
    
    that.cancelBtnTap();
  },
  cancelBtnTap: function () {
    let that = this;
    that.setData({
      alertViewShow: false
    })
  },
})